/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service.sales.impl;

import dao.sales.SalesInitDao;
import java.util.Date;
import java.util.List;
import java.util.Map;
import model.sales.SalesInit;
import net.sf.jasperreports.engine.JasperPrint;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.sales.SalesInitService;
import utility.NameValueList;
import utility.StringUtils;

/**
 *
 * @author Lalugina
 */

@Service("salesInitService")
@Transactional(readOnly=true)
public class SalesInitServiceImpl implements SalesInitService {

    private static final Logger log = Logger.getLogger(SalesInitServiceImpl.class);
      
    private String mainQuery = "select salesInit from SalesInit salesInit ";
    @Autowired private SalesInitDao dao;
    
    @Override
    @Transactional
    public boolean save(SalesInit data) {
      
        //ambil tgl server
        data.setTransDate(new Date());
        return dao.save(data);
    }

    @Override
    @Transactional
    public boolean delete(SalesInit data) {
        return dao.delete(data);
    }

    @Override
    public SalesInit get(long id) {
        return dao.getById(id);
    }

    @Override
    public SalesInit get(Map<NameValueList, Object> params) {
        String query = mainQuery+StringUtils.getFilter(params);
        return dao.getByCustomQuery(query);
    }

    @Override
    public List<SalesInit> getAll(Map<NameValueList, Object> params) {
        String query = mainQuery+StringUtils.getFilter(params);
        return dao.getAll(mainQuery);
    }

    @Override
    public List<SalesInit> getAll(int start, int num, Map<NameValueList, Object> params) {
        String query = mainQuery+StringUtils.getFilter(params);
        return dao.getAll(start, num, mainQuery);
    }

    @Override
    public JasperPrint printData(long autoId, long id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public SalesInit getCurrentInit(String pc, int shift) {
        String query = mainQuery +
        " where salesInit.closeTime is null and salesInit.pcId = " +StringUtils.fmtSQLString(pc)+
        " and salesInit.shiftId = "+shift+
        " and date(salesInit.transDate) <= "+StringUtils.truncDate(new Date());
        return dao.getByCustomQuery(query);
    }

    @Override
    public SalesInit getNextShift(String pc) {
        String query = mainQuery +
        " where salesInit.pcId = " +StringUtils.fmtSQLString(pc)+
        " and date(salesInit.transDate) = "+StringUtils.formatSqlDate(new Date());
        return dao.getByCustomQuery(query);
    }
    
}
